package com.smartleanx.module.hrm.app.empRisk;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.enums.BizTypeEnum;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeContractDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.risk.RiskTypeDO;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.eums.employee.BizFileTypeEnum;
import com.smartleanx.module.hrm.eums.risk.RiskLevelEnum;
import com.smartleanx.module.hrm.eums.risk.RiskTypeEnum;
import com.smartleanx.module.hrm.intergration.FileServiceProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeContractService;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.risk.RiskService;
import com.smartleanx.module.infra.api.file.dto.FileRefQueryDTO;
import com.smartleanx.module.infra.api.file.dto.FileRefRespDTO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 劳动合同
 *
 * @author tengcj
 * @since 2024/6/11
 */
@Component
public class RiskLaborContractHandleService implements EmployeeRiskHandle {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private EmployeeContractService employeeContractService;

    @Resource
    private RiskService riskService;

    @Resource
    private FileServiceProvider fileServiceProvider;

    public static final long RISK_ID = RiskTypeEnum.LABOR_CONTRACT.getCode();

    @Override
    public Long type() {
        return RISK_ID;
    }

    @Override
    public void handleEmployeeRisk(Set<Long> employeeIds) {
        List<EmployeeDO> employeeList = employeeService.getEmployeeList(employeeIds);
        if (CollUtil.isEmpty(employeeList)) {
            riskService.deleteRisk(employeeIds, RISK_ID);
            return;
        }
        RiskTypeDO riskType = riskService.getRiskType(RISK_ID);
        if (riskType == null) {
            throw exception(ErrorCodeConstants.RISK_TYPE_NOT_EXISTS);
        }

//        List<EmployeeContractDO> progressContractList = employeeContractService.getInProgressContractList(employeeIds);
        List<EmployeeContractDO> progressContractList = employeeContractService.getEmployeeContractList(employeeIds);

        Map<Long, List<EmployeeContractDO>> contractMap = CollectionUtils.convertMultiMap(
                progressContractList, EmployeeContractDO::getEmployeeId
        );

        // FileRefQueryDTO fileRefQueryDTO = new FileRefQueryDTO();
        // fileRefQueryDTO.setBizType(BizTypeEnum.EMPLOYEE_LABOR_CONTRACT.getValue());
        // fileRefQueryDTO.setBizFileTypes(Collections.singletonList(BizFileTypeEnum.CONTRACT_IN_SERVICE.getCode()));
        // fileRefQueryDTO.setBizRecordIds(progressContractList.stream().map(EmployeeContractDO::getId).collect(Collectors.toSet()));
        // List<FileRefRespDTO> fileRefList = fileServiceProvider.getFileRef(fileRefQueryDTO);
        // Map<Long, List<FileRefRespDTO>> fileRefMap = CollectionUtils.convertMultiMap(fileRefList, FileRefRespDTO::getBizRecordId);

        for (EmployeeDO employeeDO : employeeList) {
            if (CollUtil.isEmpty(contractMap.get(employeeDO.getId()))) {
                // // 从入职日期起，20天以内为{普通风险}、超过20天为{重大风险}；
                // LocalDateTime riskDate = employeeDO.getEntryTime().plusDays(20);
                // LocalDateTime now = LocalDateTime.now();
                // if (now.isBefore(riskDate)) {
                //     riskService.addRiskToEmployee(employeeDO.getId(), RISK_ID, RiskLevelEnum.NORMAL.getLevel());
                // } else {
                //     riskService.addRiskToEmployee(employeeDO.getId(), RISK_ID, RiskLevelEnum.MAJOR.getLevel());
                // }
                // 终止的合同不提示合同相关风险
                EmployeeContractDO lastContract = employeeContractService.getLastContract(employeeDO.getId());
                if (lastContract != null && lastContract.getTerminationStatus()) {
                    riskService.deleteRisk(employeeDO.getId(), RISK_ID);
                    continue;
                }
                riskService.addRiskToEmployee(employeeDO.getId(), RISK_ID, RiskLevelEnum.MAJOR.getLevel());
            } else {
                riskService.deleteRisk(employeeDO.getId(), RISK_ID);
            }
        }

    }

}
